/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::common::constants::AuthType;
use crate::common::ErrorCode;
use crate::log_e;
use crate::singleton_registry;

#[cfg(any(test, feature = "test-utils"))]
use mockall::automock;

/**
 * Tianshi 设计
 * 1. 迁移 SaveGlobalConfigParam GetEnableStatus QueryPinExpiredInfo 等业务逻辑
 * 2. 迁移 global_config_file_manager.c 实现的文件读写逻辑
 *
 * 读取文件可以使用 storage_io.rs
 */

// 如果set后数据有变化，数据保存到存储

#[derive(Debug, Clone)]
pub struct PinExpiredInfo {
    pub enrolled_sys_time: u64,
    pub expired_period: i64,
}

#[cfg_attr(any(test, feature = "test-utils"), automock)]
pub trait ConfigManager {
    // 如果set后数据有变化，数据保存到存储
    fn set_pin_expired_period(&mut self, expired_period: i64) -> Result<(), ErrorCode>;
    fn get_pin_expired_period(&self) -> Result<i64, ErrorCode>;
    fn get_pin_expired_info(&self, user_id: i32, auth_type: AuthType) -> Result<PinExpiredInfo, ErrorCode>;
    fn set_enable_status(
        &mut self,
        auth_type: AuthType,
        enable_status: bool,
        user_ids: &[i32],
    ) -> Result<(), ErrorCode>;
    fn get_enable_status(&self, auth_type: AuthType, user_id: i32) -> Result<bool, ErrorCode>;
    fn load_from_storage(&mut self) -> Result<(), ErrorCode>;
    fn clear(&self) -> Result<(), ErrorCode>;
}

pub struct DummyConfigManager;

impl ConfigManager for DummyConfigManager {
    fn set_pin_expired_period(&mut self, _expired_period: i64) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn get_pin_expired_period(&self) -> Result<i64, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn get_pin_expired_info(&self, _user_id: i32, _auth_type: AuthType) -> Result<PinExpiredInfo, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn set_enable_status(
        &mut self,
        _auth_type: AuthType,
        _enable_status: bool,
        _user_ids: &[i32],
    ) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn get_enable_status(&self, _auth_type: AuthType, _user_id: i32) -> Result<bool, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn load_from_storage(&mut self) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn clear(&self) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
}

singleton_registry!(ConfigManagerRegistry, ConfigManager, DummyConfigManager);
